<?php
/**
 * 城市提升-地方车展-车展广宣
 * @author jianhui
 *
 */
class CarGx extends MY_Controller{
    function CarGx(){
        parent::MY_Controller();
    }
    /**
     * 添加或修改线上广宣
     */
    function add(){
        $this->load->model('carGxModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId'       => $this->user['userId'],
                'procId'        => (int)$this->input->post('procId'),
                'projectId'     => (int)$this->input->post('projectId'),
                'mediaType1'    => (int)$this->input->post('mediaType1'),
                'mediaType1Alias'   => $this->input->post('mediaType1Alias'),
                'mediaType2'    => (int)$this->input->post('mediaType2'),
                'mediaType2Alias'   => $this->input->post('mediaType2Alias'),
                'mediaName'     => $this->input->post('mediaName'),
                'mediaSize'     => (int)$this->input->post('mediaSize'),
                'mediaSizeAlias1'   => $this->input->post('mediaSizeAlias1'),
                'mediaSizeAlias2'   => $this->input->post('mediaSizeAlias2'),
                'startTime'     => $this->input->post('startTime'),
                'endTime'       => $this->input->post('endTime'),
                'frequency'     => (int)$this->input->post('frequency'),
                'remarks'       => $this->input->post('remarks'),
                'budgetApply'   => (float)$this->input->post('budgetApply'),
                'isSync'        => 0,
            );
            $data['procId'] || showError('请选择主线流程');
            $data['projectId'] || showError('请选择车展');
            $data['mediaType1'] || showError('请选择一级媒体类型');
            if ($data['mediaType1'] == -1 && !$data['mediaType1Alias']){
                showError('请输入其它媒体类型');
            }
            if ($data['mediaType1'] != -1 && !$data['mediaType2']){
                showError('请选择二级媒体类型');
            }
            $data['mediaName'] || showError('请输入媒体名称');
            if ($data['mediaType1'] == 1){
                $data['mediaSize'] || showError('请选择投放规格');
                //其它投放规格
                if ($data['mediaSize'] == -1 && !$data['mediaSizeAlias1']){
                    showError('请输入其它规格');
                }
                //报纸版面限制
                if ($data['mediaType2'] == 1 && !$data['mediaSizeAlias2']){
                    showError('请输入报纸版面');
                }
            }elseif (in_array($data['mediaType1'], array(3))){
                $data['mediaSizeAlias1'] = (float)$data['mediaSizeAlias1'];
                $data['mediaSizeAlias2'] = (float)$data['mediaSizeAlias2'];
                $data['mediaSizeAlias1'] || showError('请输入长度');
                $data['mediaSizeAlias2'] || showError('请输入高度');
            }else{
                $data['mediaSizeAlias1'] || showError('请输入投放规格');
            }
            $data['startTime'] || showError('请选择投放开始时间');
            $data['endTime'] || showError('请选择投放结束时间');
            $data['budgetApply'] >= 0 || showError('请输入预算费用');
            if ($id){
                $id = $this->carGxModel->edit($id, $data);
            }else{
                $data['createTime'] = time();
                $id = $this->carGxModel->add($data);
            }
            if ($id){
                showSuccess('保存广宣成功');
            }else{
                showError('保存广宣失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            if ($id){
                $gxData = $this->carGxModel->getData(array('id'=>$id, 'storeId'=>$this->user['userId']));
                $gxData || showError('车展广宣数据不存在');
                in_array($gxData['budgetAuditRs'], array(-1,0,3)) || showError('项目已审核，不允许修改');
                $this->viewData['gxData'] = $gxData;
                $procId = $gxData['procId'];
                $projectId = $gxData['projectId'];
            }else{
                $procId = (int)$this->input->get('procId');
                $procId || showError('请选择主线流程');
                $projectId = (int)$this->input->get('projectId');
                $projectId || showError('请选择车展ID');
            }
            $this->viewData['procId'] = $procId;
            $this->viewData['projectId'] = $projectId;
            //主线流程数据
            $this->load->model('carProcessModel');
            $procData = $this->carProcessModel->getData($this->viewData['procId']);
            $procData || showError('主线流程数据不存在');
            $this->viewData['procData'] = $procData;
            //是否为特殊补报
            $this->viewData['isBubao'] = $this->auth->isCarBudgetEdit($procData);
            //车展数据
            $this->load->model('carProjectModel');
            $carshowData = $this->carProjectModel->getData($this->viewData['projectId']);
            $carshowData || showError('车展数据不存在');
            $this->viewData['carshowData'] = $carshowData;
            $this->load->view('CarGx/add', $this->viewData);
        }
    }
    /**
     * 删除广宣
     */
    function del(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要删除的线上广宣');
        $this->load->model('carGxModel');
        if ($this->carGxModel->del(array('id'=>$id, 'storeId'=>$this->user['userId']) )){
            showSuccess('删除广宣成功');
        }else{
            showError('删除广宣失败');
        }
    }
}